package com.chixing.ih.module.programmer.controller;

import com.chixing.ih.json.ResponseData;
import com.chixing.ih.module.programmer.pojo.WorkExper;
import com.chixing.ih.module.programmer.service.IProgrammerService;
import com.chixing.ih.module.programmer.service.IWorkExperService;
import com.chixing.ih.util.RequestIdUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @version 1.0
 * @Author MasterYan
 * @Date 2020/10/3
 * Time: 1:40
 */
@RestController
@RequestMapping("/workExper")
@Api(tags = "程序员模块工作经历相关Api")
public class WorkExperController {
    @Autowired
    IWorkExperService workExperService;

    @Autowired
    IProgrammerService programmerService;

    /**
     * desc:批量保存工作经历接口
     *
     * @param workExpers
     * @return
     */
    @ApiOperation(value = "批量保存工作经历接口", notes = "批量保存工作经历")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工作经历id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "beginTime", value = "工作开始日期", dataType = "date"),
            @ApiImplicitParam(name = "endTime", value = "工作结束日期", dataType = "date"),
            @ApiImplicitParam(name = "company", value = "公司名称", dataType = "string"),
            @ApiImplicitParam(name = "position", value = "职位", dataType = "string"),
            @ApiImplicitParam(name = "workExperDesc", value = "工作经历描述", dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 201, message = "更新成功"),
            @ApiResponse(code = 400, message = "更新失败"),
    })
    @PutMapping("/workExperList")
    public Map<String, Object> saveListExpers(@RequestBody List<WorkExper> workExpers, HttpServletRequest request) {
        Long myId = RequestIdUtil.getMyId(request);
        boolean flag = workExperService.updateList(workExpers);
        if (flag) {
            programmerService.updateProgrammerTime(myId);
            return ResponseData.REP_CREATED_SUCCESS();
        } else
            return ResponseData.REP_INVALID_ERROR();

    }

    /**
     * desc:查询所有工作经历接口
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查询所有工作经历接口", notes = "查询所有工作经历")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "程序员id", required = true, dataType = "long"),

    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功")

    })
    @GetMapping("/workExperList/{userId}")
    public Map<String, Object> listWorkExpers(@PathVariable("userId") long userId, HttpServletRequest request) {
        List<WorkExper> workExpers = workExperService.listAll(userId);
        return ResponseData.REP_GET_SUCCESS(workExpers);
    }

    /**
     * desc:获取新增工作经历id接口
     *
     * @return
     */
    @ApiOperation(value = "获取新增工作经历id接口", notes = "获取新增工作经历id")
    @ApiResponses({
            @ApiResponse(code = 200, message = "保存成功"),
            @ApiResponse(code = 400, message = "保存失败"),
    })
    @PostMapping("/workExper")
    public Map<String, Object> saveWorkExper(HttpServletRequest request) {
        Long myId = RequestIdUtil.getMyId(request);
        WorkExper workExper = new WorkExper();
        workExper.setUserId(myId);
        WorkExper workExper1 = workExperService.saveList(workExper);
        if (workExper1 != null) {
            programmerService.updateProgrammerTime(myId);
            return ResponseData.REP_GET_SUCCESS(workExper1);
        } else
            return ResponseData.REP_INVALID_ERROR();

    }

    /**
     * desc:删除一条工作经历接口
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一条工作经历接口", notes = "删除一条工作经历")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工作经历id", required = true, dataType = "long")
    })

    @ApiResponses({
            @ApiResponse(code = 201, message = "删除成功"),
            @ApiResponse(code = 400, message = "删除失败"),
    })
    @DeleteMapping("/workExper/{id}")
    public Map<String, Object> deleteWorkExper(@PathVariable("id") long id, HttpServletRequest request) {
        Long myId = RequestIdUtil.getMyId(request);
        boolean flag = workExperService.deleteWorkExper(id);
        if (flag) {
            programmerService.updateProgrammerTime(myId);
            return ResponseData.REP_CREATED_SUCCESS();
        } else
            return ResponseData.REP_INVALID_ERROR();

    }


}

//    select any_value(u.user_id) user_id,any_value(u.nick_name)nick_name,any_value(i.image_url)image_url,any_value(u.user_company)user_company,any_value(u.user_career)user_career,any_value(w.salary)salary,any_value(w.region_city)region_city,any_value(w.working_day_begin_time)working_day_begin_time,any_value(w.working_day_end_time)working_day_end_time,any_value(w.weekend_working_begin_time)weekend_working_begin_time,any_value(w.weekend_working_end_time)weekend_working_end_time,any_value(truncate(sum(attitude_score+punctuality_score+skill_score)/count(1)/3,1))star
//        from tbl_user u LEFT JOIN
//        tbl_work w on u.user_id=w.user_id LEFT JOIN
//        tbl_user_image i ON u.user_id=i.user_id LEFT JOIN
//        tbl_evaluate e on w.user_id=e.user_evaluated_id
//        GROUP BY u.user_id
//        ORDER BY star

